﻿@{
    if (false)
    {
        <script src="~/LiberUI/Scripts/jquery.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Base.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Action.js"></script>
    }
}
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <meta name="renderer" content="webkit">
    <title>新销售订单</title>
    <link href="/LiberUI/Style/LiberUI.css" rel="stylesheet" />
    <link href="/SiteUI/Base.css" rel="stylesheet" />
    <link href="/SiteUI/Sale/WxNewOrders.css" rel="stylesheet" />
    <script src="/LiberUI/Scripts/jquery.js"></script>
    <script src="/LiberUI/Scripts/LiberUI.Base.js"></script>
    <script src="/LiberUI/Scripts/LiberUI.Action.js"></script>
    <script src="/LiberUI/Scripts/LiberUI.js"></script>
    <script src="/LiberUI/Scripts/laytpl.js"></script>
    <script src="/SiteUI/res_sobiz.js"></script>
</head>
<body>
    <div class="view screen">
        <div class="row">
            <div class="clo-sm-12 detail">

            </div>
        </div>
        <div class="row foot">
            <div class="col-sm-6 button" id="btnAction">
                确认订单
            </div>
            <div class="col-sm-2 nomore" id="naviPrev">
                <span><i class="icon-navigatebefore"></i>上一张</span>
            </div>
            <div class="col-sm-2">
                <span id="order-index"></span>
                <span>/</span>
                <span id="order-count"></span>
            </div>
            <div class="col-sm-2" id="naviNext">
                <span>下一张<i class="icon-navigatenext"></i></span>
            </div>
        </div>
    </div>
    <script id="tplOrderDetail" type="text/html">
        <div class="row" id="orderHead">
            <div class="col-sm-12">
                <h4>交易编号：<strong id="oh-id">{{d.idstr}}</strong></h4>
            </div>
            <div class="col-sm-12">
                <h4>总金额：<strong id="oh-amount">{{d.amountstr}}</strong></h4>
            </div>
            <div class="col-sm-12">
                <h4>状态:<span id="oh-status" class="Pendding"><i>订单状态</i></span></h4>
            </div>
            <div class="col-sm-12">
                <h5>客户订单：<strong id="oh-clnt-serial"></strong></h5>
            </div>
            <div class="col-sm-12">
                <h5>订货日期：<strong id="oh-order-date">00001</strong></h5>
            </div>
        </div>

        <!--订单详情-->
        <div class="row">
            <div class="InfoSectionBody col-sm-12" id="tradeInfo">
                <div class="row ti-body">
                      <div id="orderDelivery">
                    <span>已发货：</span>
                    <span>已入库：</span>
                </div>
                    <div class="col-sm-12">
                        <h5>买方信息</h5>
                        <div class="DataField">
                            <span class="df-title">采&nbsp;&nbsp;购&nbsp;&nbsp;商：</span>
                            {{#if(d.client_realname){ }}
                            <span class="df-value"><a href="/Company/Portal?oid={{d.client_idstr}}">{{d.client_realname}}</a></span>
                            {{#}else{ }}
                            <span class="df-value"><a href="#">{{d.src_client}}</a></span>
                            {{#} }}
                        </div>
                        <div class="DataField">
                            <span class="df-title">联&nbsp;&nbsp;系&nbsp;&nbsp;人：</span>
                            {{#if(d.buyer_realname){ }}
                            <span class="df-value"><a href="#">{{d.buyer_realname}}</a></span>
                            {{#}else{ }}
                            <span class="df-value"><a href="#">{{d.src_buyer}}</a></span>
                            {{#} }}
                        </div>
                        <div class="DataField">
                            <span class="df-title">联系方式：</span>
                            <span class="df-value">{{d.buyer_phone}}</span>
                        </div>
                    </div>
                    <div class="col-sm-12">
                        <h5>交付信息</h5>
                        <div class="DataField">
                            <span class="df-title">收&nbsp;&nbsp;货&nbsp;&nbsp;人：</span>
                            <span class="df-value">
                                <span>{{d.consignee_displayname}}</span>
                                <span>{{d.consignee_phone}}</span>
                            </span>
                        </div>
                        <div class="DataField">
                            <span class="df-title">交付方式：</span>
                            <span class="df-value">{{d.deliverymode}}</span>
                        </div>
                        <div class="DataField">
                            <span class="df-title">送货地址：</span>
                            <span class="df-value">{{d.src_dlvaddress}}</span>
                        </div>
                        <div class="DataField">
                            <span class="df-title">送货时间：</span>
                            {{#if(d._urgent){ }}
                            <span class="df-value urgent" title="交货日期已临近，请注意跟进">{{d._dlvDate}}<i class="icon-erroroutline"></i></span>
                            {{#}else{ }}
                            <span class="df-value">{{d._dlvDate}}</span>
                            {{#} }}
                        </div>
                        <div class="DataField">
                            <span class="df-title">质保条件</span>
                            <span class="df-value">{{d.qualityguarantee}}</span>
                        </div>
                    </div>
                    <div class="col-sm-12">
                        <h5>支付和发票信息</h5>
                        <div class="DataField">
                            <span class="df-title">支付方式：</span>
                            <span class="df-value">{{d.settlement}}</span>
                        </div>
                        <div class="DataField">
                            <span class="df-title">发票抬头：</span>
                            <span class="df-value">{{d.invoiceheading}}</span>
                        </div>
                        <div class="DataField">
                            <span class="df-title">发票类型：</span>
                            <span class="df-value">增值税发票</span>
                        </div>
                        <div class="DataField">
                            <span class="df-title">税&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;率：</span>
                            <span class="df-value">17%</span>
                        </div>
                        <div class="DataField">
                            <span class="df-title">发票内容：</span>
                            <span class="df-value">明细</span>
                        </div>
                    </div>
                    <div class="col-sm-12">
                        <h5>其他信息</h5>
                        {{#if(d.src_orderserial){ }}
                        <div class="DataField">
                            <span class="df-title">客户订单：</span>
                            <span class="df-value">{{d.src_orderserial}}{{d.src_orderversion}}</span>
                        </div>
                        {{#} }}
                        <div class="DataField">
                            <span class="df-title">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注：</span>
                            <span class="df-value">{{d.memo}}</span>
                        </div>
                    </div>
                </div>
            </div>
        </div> 

        <!--订单明细-->
        <div class="InfoSection row expand" id="orderItems">
            <div class="InfoSectionTitle">
                <i class="soicon "></i><span>商品明细</span>
            </div>
            <div class="InfoSectionBody">
                <ol></ol>
                <li class="order-item-tpl">
                    <h5 class="oi-title"></h5>
                    <span class="oi-model"></span>
                    <br />
                    <span class="oi-productSpec"></span>
                    <div class="oi-mx-countinfo">
                        <span class="oi-mx-price">单价：</span>
                        <span class="oi-storage">入库：-</span>
                        <br />
                        <span class="oi-mx-count">数量：</span>
                        <span class="oi-mx-amount">小计：</span>
                    </div>
                    <div class="floatClear">

                    </div>
                </li>
            </div>
        </div>
    </script>
    <script>
        var orders = [];
        var cIndex = -1;
        var tmpDoc = document.createDocumentFragment();
        var tplNO = null;
        var btnAction = null;
        
        function goPrev() {
            if (cIndex <=0) {
                return;
            }
            LoadDetail(cIndex - 1);
            $("#order-index").vl(cIndex+1);
            if (cIndex == 0) {
                $("#naviPrev").addClass("nomore");
            }
            else {
                $("#naviPrev").removeClass("nomore");
            }
            if (cIndex == orders.length-1) {
                $("#naviNext").addClass("nomore");
            }
            else {
                $("#naviNext").removeClass("nomore");
            }
        }
        function goNext() {
            if (cIndex >= orders.length-1) {
                return;
            }
            LoadDetail(cIndex+1);
            $("#order-index").vl(cIndex + 1);
            if (cIndex == 0) {
                $("#naviPrev").addClass("nomore");
            }
            else {
                $("#naviPrev").removeClass("nomore");
            }

            if (cIndex == orders.length-1) {
                $("#naviNext").addClass("nomore");
            }
            else {
                $("#naviNext").removeClass("nomore");
            }
        }

        function onGetNewOrderEnd(rtn) {
            if (rtn.IsPositive) {
                orders = rtn.Data.Orders;
                if (orders.length > 0) {
                    var oid = getUrlParam("oid");
                    var co = orders.findObjIndex("idstr", oid);
                    if (co == -1) {
                        co = 0;
                    }
                    $("#order-index").vl(co+1);
                    $("#order-count").vl(orders.length);
                    LoadDetail(co);
                }
            }
        };
        function LoadDetail(index,refresh) {
            if (index >= 0 && index < orders.length) {
                var od = orders[cIndex];
                //检查当前视图
                if (od != null && od.$View) {
                    od.$View.appendTo(tmpDoc);
                }
                //处理目标视图
                cIndex = index;
                od = orders[cIndex];
                if (od.$View&&!refresh) {
                    od.$View.prependTo(".detail");
                }
                else {
                    $("<div class='col-sm-12 orderView loading'><i class='soicon'></i>正在加载订单内容...</div>").prependTo(".detail");
                    $.getJSON("/Trade/GetOrder", { 'oid': od.idstr }, function (rtn) {
                        if (rtn.IsPositive) {
                            var to = orders.findObj("idstr", rtn.Data["idstr"],rtn.Data);
                            if (to != null) {
                                renderOrder(to);
                            }
                        }
                    });
                }

                //处理目标订单的状态对应的操作
                if (od.status == 'Pending') {
                    btnAction.vl("确认订单").enable();
                }
                else {
                    btnAction.vl("已确认").disable();
                }
            }
        };
        function renderOrder(od) {
            od.amountstr = od.amount.roundTo(2).toThousands();
            od.taxrate = od.taxrate + "%";
            od.consignee_displayname = od.src_consignee||od.consignee;
            od._dlvDate = new Date(od.src_deliverydate || od.deliverydate).format("YYYY/MM/DD");
            if (od.src_orderserial) {
                if (od.src_orderversion) {
                    od.src_orderserial += '[' + od.src_orderversion + ']';
                }
            }
            else {
                od.src_orderserial = "-";
            }

            var v = tplNO.render(od);
            $(".detail").children().remove();

            od.$View = $(v).prependTo(".detail");
            $("#oh-status").addClass(od.status);
            $("#oh-status").vl("<i></i>" + res_sobiz.var.Sale[od.status]);
            $("#oh-clnt-serial").vl(od.src_orderserial);
            $("#oh-order-date").vl(new Date(od.src_orderdate || od.createtime).format("YYYY/MM/DD"));
            renderOrderMX(od);
        };
        function renderOrderMX(order) {
            //#region 处理订单明细
            if (order.OrderMX) {
                var tplMX = $(".order-item-tpl").eq(0).hide();
                var gridBody = $("#orderItems").find("ol").eq(0);
                if (gridBody.children("li").length <= 1) {
                    order.OrderMX.forEach(function (i) {
                        order.am += i.amount;
                        order.tdlv += ($.isNumeric(i.totaldeliveried) ? i.totaldeliveried : 0) * i.price;
                        order.tstlkin += ($.isNumeric(i.totalstockin) ? i.totalstockin : 0) * i.price;
                        var row = tplMX.clone();
                        row.children(".oi-title").vl(i.brand + '【' + i.product + '】');
                        row.children(".oi-model").vl("型号："+i.model);
                        row.children(".oi-productSpec").vl("规格：" + i.src_specification || "-");
                        row.find(".oi-mx-price").vl("单价：" + i.price);
                        row.find(".oi-mx-count").vl("数量："+i.count + '[' + i.unit + ']');
                        row.find(".oi-mx-amount").vl("小计：" + i.amount.toFixed(2));
                        row.appendTo(gridBody).fadeIn();
                    }); 
                    $("#orderDelivery").children("span").eq(0).vl("已发货：" + (order.tdlv / order.am).formatPercent(2));
                    $("#orderDelivery").children("span").eq(1).vl("已入库：" + (order.tstlkin / order.am).formatPercent(2));
                }
            }
            //#endregion
        };
        function confirmOrder() {
            if(cIndex>=0&&cIndex<orders.length){
                var od = orders[cIndex];
                $.getJSON("/Sale/ConfirmOrder", { oid: od.idstr }, function (rtn) {
                    if (rtn.IsPositive) {
                        LoadDetail(cIndex, true);
                    }
                    else {
                        btnAction.vl("确认订单").enable();
                    }
                });
            }
        };
        onInsecClick = function (ev) {
            $(this).parent(".InfoSection").toggleClass("expand");
        };

        $(function () {
            //获取新订单数量
            $.getJSON("/Sale/QueryOrders", {onlymine:false,status:'pending'}, onGetNewOrderEnd);

            $(".detail").delegate(".InfoSectionTitle", "click", onInsecClick);
            $("#naviPrev").on("click", goPrev);
            $("#naviNext").on("click", goNext);
            tplNO = laytpl($("#tplOrderDetail")[0].innerHTML);
           
            btnAction = new LiberUI.Button("#btnAction")
                                   .on("LCclick", confirmOrder);
        })
    </script>
</body>
</html>
